<?php

/**
 * @author PhatPham
 * @copyright 2013
 */
class AnswerRelationmodel extends CI_Model {
    var $template_id;   //atid
    var $question_id;   //qid
	var $attribute;	//attr
    var $created_by_user_id;    //userid
    var $created_on_date;
    var $last_modified_by_user_id;  //userid
    var $last_modified_on_date;
    
    function __construct()
    {
        // Call the Model constructor
        $this->load->database();
    }
    //Author PhatPham
    //Get answer relations list of question
    function get_answer_relations_list($atid){
        $query = $this->db->query("select ar.template_id, ar.question_id, ar.attribute, 
		ar.created_by_user_id, ar.created_on_date, ar.last_modified_by_user_id, ar.last_modified_on_date
		from sur_answer_relation ar, sur_question q 
		where ar.question_id = q.question_id and ar.template_id = '".$atid."' 
		order by q.view_order asc");
        return $query;
    }
    //Author PhatPham
    //Create answer relation
    function create_answer_relation($atid, $qid, $attr, $userid){
        $data = array(
				'template_id' => $atid,
				'question_id' => $qid,
				'attribute' => $attr,
				'created_by_user_id' => $userid,
				'created_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
        $this->db->insert('sur_answer_relation', $data);
    }
	//Author PhatPham
    //Create answer relation
    function edit_answer_relation_value($atid, $qid, $attr, $userid){
        $data = array(
				'template_id' => $atid,
				'question_id' => $qid,
				'attribute' => $attr,
				'last_modified_by_user_id' => $userid,
				'last_modified_on_date' => mdate('%m/%d/%Y %H:%i:%s',now())
            );
		$this->db->where(array('template_id' => $atid, 'question_id' => $qid));
        $this->db->update('sur_answer_relation', $data);
    }
	//Author: PhatPham
    //Delete 1 answer relation
    function delete_answer_relation($atid, $qid){
        $data = array(
               'template_id' => $atid,
			   'question_id' => $qid
            );
        $this->db->delete('sur_answer_relation', $data);
    }
    //Author: PhatPham
    //Delete answer relations list
    function delete_answer_relations_list($atid){
        $data = array(
               'template_id' => $atid
            );
        $this->db->delete('sur_answer_relation', $data);
    }
}
?>